﻿using suirui.ZhuMu.Model;
using suirui.ZhuMu.Entity;
using suirui.ZhuMu.Entity.Entities;
using System;
using System.Linq;
using System.Threading.Tasks;
using suirui.ZhuMu.Repositories;
using System.Collections.Generic;
using suirui.ZhuMu.Common.Helper;
using suirui.ZhuMu.Common;
using System.Text;
using suirui.ZhuMu.Log;
using System.IO;

namespace suirui.ZhuMu.Services
{
    public class UserService : BaseServices<User>, IUserService
    {
        private readonly IUserRepository _UserRepository;
        public UserService(IUserRepository UserRepository)
            : base(UserRepository)
        {
            this._UserRepository = UserRepository ?? throw new ArgumentNullException(nameof(UserRepository));
        }

        public async Task<APIResult<PageData<User>>> GetUserList(GetListByUserDto paramsDto)
        {
            PageData<User> result = await _UserRepository.GetUserList(paramsDto);
            return OperateResult.QuerySuccessInfo(result);
        }

        public async Task<APIResult<User>> GetUserById(long Id)
        {
            User result = await GetAsync(t => t.UserId == Id);
            return OperateResult.QuerySuccessInfo(result);
        }

        public async Task<APIResult<User>> GetUserByEmail(string Email)
        {
            User result = await GetAsync(t => t.Email == Email);            
            return OperateResult.QuerySuccessInfo(result);
        }

        public async Task<APIResult> AddUser(User user)
        {
            var result = await _UserRepository.AddUser(user);            
            return result ? OperateResult.OperateSuccessInfo(result) : OperateResult.OperateFailInfo<User>();
        }

        public async Task<APIResult> UpdateUser(User user)
        {
            var result = await _UserRepository.UpdateUser(user);
            return result ? OperateResult.OperateSuccessInfo(result) : OperateResult.OperateFailInfo<User>();
        }

        public async Task<APIResult> DeleteUser(long Id)
        {
            var user = Get(t => t.UserId == Id);
            //user.Status = 0;
            var result = await UpdateAsync(user);
            return result ? OperateResult.OperateSuccessInfo(result) : OperateResult.OperateFailInfo<User>();
        }

        public APIResult<bool> IsExistReMail(string mail)
        {
            User result1 = null;
            User result2 = null;
            //验证邮件大小写一样不能进行注册
            var strMail = DESUtils.DecryptString(mail);
            if (!string.IsNullOrEmpty(strMail))
            {
                string lowMail = DESUtils.EncryptString(strMail.ToLower());
                result1 = Get(t => t.Email == lowMail);
                string uppMail = DESUtils.EncryptString(strMail.ToUpper());
                result2 = Get(t => t.Email == uppMail);
            }
            //var result = Get(t => t.Email == mail);
            bool result = result1 != null || result2 != null;
            return new APIResult<bool>() { Data = result };
        }
    }
}
